/**
 * Created by Dean on 2018-5-9.
 */
class Toast{




    //默认配置常量
    //toast的显示时长
    public static LONG_TIME: number = 5000;
    public static SHORT_TIME: number = 2000;
    //toast在舞台中的位置
    public static LAYOUT_TOP: number = 0;
    public static LAYOUT_CENTER: number = 1;
    public static LAYOUT_BOTTOM: number = 2;

    //容器
    private static toast_sprite: egret.Sprite;
    //显示时长
    private static config: any;
    //舞台
    private static stage:egret.Stage;

    private static toastID:number = 0;

    private static _parent:egret.Sprite = null;


    private static default_config:any = {
        position:Toast.LAYOUT_CENTER,
        time:Toast.SHORT_TIME,
        allow:true , //toast显示的时候是否允许其他的点击事件
        callback:null,  //toast结束时的回调函数
        background:'', //背景，如果不为空，则调用util.getBitmapByname的方法加载一张图片
        text:''
    };


    /**
     * 添加一个容器层至舞台，与dialog区分显示等级
     */
    public static init():void{

        if(!Toast.stage){
            Toast.stage = egret.MainContext.instance.stage;
        }

        if(!Toast._parent){
            Toast._parent = new egret.Sprite();
            Toast._parent.width = Toast.stage.stageWidth;
            Toast._parent.height = Toast.stage.stageHeight;
            Toast.stage.addChild(Toast._parent);
        }

    }


    public static makeText(conf:any):Toast{

        if(!Toast._parent){
            console.error('请先初始化！！');
            return;
        }

        for(let key in Toast.default_config){
            if(!conf.hasOwnProperty(key)){
                conf[key] = Toast.default_config[key];
            }
        }
        Toast.config = conf;
        if(Toast._parent.contains(Toast.toast_sprite)){
            Toast.toast_sprite.graphics.clear();
            Toast.toast_sprite.removeChildren();
            Toast._parent.removeChild(Toast.toast_sprite);
        }
        Toast.toast_sprite = new egret.Sprite();
        Toast.toast_sprite.alpha = 1;
        let text = new egret.TextField();
        text.text = conf.text;
        text.size = 30;
        text.verticalAlign = egret.VerticalAlign.MIDDLE;
        text.lineSpacing = 10;
        text.textColor = 0xFFFFFF;

        if(text.width > 700){
            text = utils.createTextField({
                width:700,
                textAlign:egret.HorizontalAlign.LEFT,
                lineSpacing:20,
                text:conf.text,
                textColor:0xFFFFFF,
                stroke:1,
            });
            Toast.toast_sprite.height = text.height + 20;
            text.y = 10;
        }

        Toast.toast_sprite.width = text.width + 100;
        Toast.toast_sprite.height = text.height + 40;
        Toast.toast_sprite.anchorOffsetX = Toast.toast_sprite.width/2;
        Toast.toast_sprite.anchorOffsetY = Toast.toast_sprite.height/2;
        text.x = Toast.toast_sprite.width / 2 - text.width / 2;
        text.y = Toast.toast_sprite.height/2 - text.height/2;
        if(conf.background != ''){
            let background:egret.Bitmap = utils.createBitmapByName(conf.background);
            background.width = Math.max(Toast.toast_sprite.width,background.width);
            background.height = Math.max(Toast.toast_sprite.height,background.height);
            background.x = Toast.toast_sprite.width/2 - background.width/2;
            background.y = Toast.toast_sprite.height/2 - background.height/2;
            Toast.toast_sprite.addChild(background);
        }else{
            Toast.toast_sprite.graphics.lineStyle(4,0xFDDC22);
            Toast.toast_sprite.graphics.beginFill(0x000000,0.66);
            Toast.toast_sprite.graphics.drawRoundRect(0, 0, Toast.toast_sprite.width, Toast.toast_sprite.height, 30, 30);
            Toast.toast_sprite.graphics.endFill();
        }


        //指定toast的位置
        if (conf.position == Toast.LAYOUT_BOTTOM) {
            Toast.toast_sprite.y = Toast.stage.stageHeight - Toast.toast_sprite.height - 150;
        } else if (conf.position == Toast.LAYOUT_CENTER) {
            Toast.toast_sprite.y = Toast.stage.stageHeight / 2 ;
        } else{
            Toast.toast_sprite.y = 150;
        }
        var stageW:number = this.stage.stageWidth;
        Toast.toast_sprite.addChild(text);

        Toast.toast_sprite.x = stageW / 2;




        return new Toast();
    }

    public show():void{
        if(Toast.toast_sprite){
            if(!Toast.toast_sprite.hasEventListener(egret.Event.REMOVED_FROM_STAGE)){
                Toast.toast_sprite.addEventListener(egret.Event.REMOVED_FROM_STAGE,Toast.removedFromStage,this);
            }
            Toast.toast_sprite.scaleX = 0.4;
            Toast.toast_sprite.scaleY = 0.4;
            Toast._parent.addChild(Toast.toast_sprite);
            egret.Tween.get(Toast.toast_sprite).to({scaleX:1,scaleY:1},200).wait(Toast.config.time).to({alpha: 0,scaleX:0.4,scaleY:0.4}, 300).call(function () {
                if (Toast._parent.contains(Toast.toast_sprite)) {
                    Toast._parent.removeChild(Toast.toast_sprite);
                    if(Toast.config.callback instanceof Function){
                        Toast.config.callback();
                    }
                }
            }, this);
        }
    }


    private static removedFromStage():void{
        if(Toast.toast_sprite){
            Toast.toast_sprite.removeChildren();
            egret.Tween.removeTweens(Toast.toast_sprite);
            Toast._parent.touchChildren = true;
        }
    }



}